package cn.rokhdelar.ams.management.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.rokhdelar.ams.management.domain.po.SysAuthorityCategory;
import cn.rokhdelar.ams.management.domain.vo.AuthorityCategoryVO;
import cn.rokhdelar.ams.management.service.SysAuthorityCategoryService;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Rokhdelar
 * @since 2025/3/26 -- 15:52
 **/
@RequiredArgsConstructor
@RestController
@RequestMapping("/management/authorityCategories")
public class AuthorityCategoryController {
    final SysAuthorityCategoryService sysAuthorityCategoryService;

    @GetMapping
    @Schema(description = "获取所有权限目录")
    public ResponseEntity<List<AuthorityCategoryVO>> getAll() {
        List<SysAuthorityCategory> authorityCategoryList = sysAuthorityCategoryService.list();
        return ResponseEntity.ok(BeanUtil.copyToList(authorityCategoryList, AuthorityCategoryVO.class));
    }

    @GetMapping("/{id}")
    @Schema(description = "通过ID获取权限目录")
    public ResponseEntity<AuthorityCategoryVO> getById(@PathVariable String id) {
        SysAuthorityCategory authorityCategory = sysAuthorityCategoryService.getById(id);
        return ResponseEntity.ok(BeanUtil.copyProperties(authorityCategory, AuthorityCategoryVO.class));
    }

    @GetMapping("/tree")
    @Schema(description = "获取权限树形结构数据")
    public ResponseEntity<List<AuthorityCategoryVO>> getTree() {
        return ResponseEntity.ok(BeanUtil.copyToList(sysAuthorityCategoryService.getTree(), AuthorityCategoryVO.class));
    }

    @PostMapping
    @Schema(description = "创建新的权限目录")
    public ResponseEntity<Boolean> create(@RequestBody SysAuthorityCategory sysAuthorityCategory) {
        return ResponseEntity.ok(sysAuthorityCategoryService.save(sysAuthorityCategory));
    }

    @PatchMapping
    @Schema(description = "更新权限目录")
    public ResponseEntity<Boolean> update(@RequestBody SysAuthorityCategory sysAuthorityCategory) {
        return ResponseEntity.ok(sysAuthorityCategoryService.updateById(sysAuthorityCategory));
    }

    @DeleteMapping("/{id}")
    @Schema(description = "删除权限目录")
    public ResponseEntity<Boolean> delete(@PathVariable String id) {
        return ResponseEntity.ok(sysAuthorityCategoryService.removeById(id));
    }

}
